package com.spring.sunflower.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.spring.sunflower.R;

/**
 * Created by Kevin on 2021/8/23<br/>
 * <p>
 * Describe:<br/>
 */
public class MsgItemView extends ConstraintLayout {

    private int localAvatarUrl;
    private String remoteAvatarUrl;
    private String name, lastMsg, time, msgCount;
    private boolean showMsgCount;
    private RoundedImageView rivCover;
    private TextView tvName;
    private TextView tvTime;
    private TextView tvLastMsg;
    private TextView tvMsgCount;
    private Context context;

    public MsgItemView(Context context) {
        this(context, null);
    }

    public MsgItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MsgItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MessageItemView);
        if (typedArray != null) {
            localAvatarUrl = typedArray.getInt(R.styleable.MessageItemView_localAvatar, R.drawable.ic_placeholder);
            remoteAvatarUrl = typedArray.getString(R.styleable.MessageItemView_remoteAvatar);
            name = typedArray.getString(R.styleable.MessageItemView_name);
            lastMsg = typedArray.getString(R.styleable.MessageItemView_lastMsg);
            time = typedArray.getString(R.styleable.MessageItemView_time);
            msgCount = typedArray.getString(R.styleable.MessageItemView_msgCount);
            showMsgCount = typedArray.getBoolean(R.styleable.MessageItemView_msgCountVisible, false);
            typedArray.recycle();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_message_item_view, null);
        rivCover = view.findViewById(R.id.ivCover);
        tvName = view.findViewById(R.id.tvName);
        tvTime = view.findViewById(R.id.tvTime);
        tvLastMsg = view.findViewById(R.id.tvLastMessage);
        tvMsgCount = view.findViewById(R.id.tvMessageCount);
        if (TextUtils.isEmpty(remoteAvatarUrl)) {
            Glide.with(context)
                    .load(localAvatarUrl)
                    .placeholder(R.drawable.ic_placeholder)
                    .into(rivCover);
        } else {
            Glide.with(context)
                    .load(remoteAvatarUrl)
                    .placeholder(R.drawable.ic_placeholder)
                    .into(rivCover);
        }
        tvName.setText(name);
        tvTime.setText(time);
        tvLastMsg.setText(lastMsg);
        tvMsgCount.setText(msgCount);
        if (showMsgCount) {
            tvMsgCount.setVisibility(View.VISIBLE);
        } else {
            tvMsgCount.setVisibility(View.GONE);
        }
        addView(view);
    }

    public void setLocalAvatarUrl(int resId) {
        Glide.with(context)
                .load(resId)
                .placeholder(R.drawable.ic_placeholder)
                .into(rivCover);
//        invalidate();
    }
    public void setRemoteAvatarUrl(String url){
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.ic_placeholder)
                .into(rivCover);
    }

    public void setLastMsg(String str) {
        tvLastMsg.setText(str);
//        invalidate();
    }

    public void showMsgCount(boolean b) {
        if (b) {
            tvMsgCount.setVisibility(VISIBLE);
        } else {
            tvMsgCount.setVisibility(GONE);

        }
//        invalidate();
    }

    public void setMsgCount(String str) {
        tvMsgCount.setText(str);
//        invalidate();
    }
    public void setMsgTime(String str){
        tvTime.setText(str);
    }

}
